import { baseClass } from "./baseClass";
import { BaseBody } from "../rpg/entity/body_base";
import common from "./common";
import client_component from "../../component/client_component";
import intent_component from "../../component/intent_component";
import { mathType } from "./face";
import { Icard } from "./Icard";
import { ButtonConfig, IntentHelper } from "./intentHelper";
import sdk from '../index';
import { DIContainer } from '../../infrastructure/di';

export enum ComType{
    base = 'base',
    client = 'client',
    rpg = 'rpg',
    位置 = 'pos',
    背包 = 'bag',
    死亡掉落 = 'dieReward',
    装备栏 = 'equip',
    意图 = 'intent',

    对话框 = 'pop',
    随从 = 'follow',
    创建数据 = 'create',
    血统 = 'talent',
    邮件 = 'mail',
    管理员 = 'admin',
    签到 = 'sign',
    任务 = 'task',
    排行榜 = 'ranking',
}

export default class component extends baseClass{
    public id:string = sdk.tool.common.uuid;
    public name!:string;
    private clsName:string;
    private _body!:BaseBody;
    private _master:BaseBody | null = null;
    
    /** DI容器实例，用于服务注入 */
    protected static container: DIContainer | null = null;
    
    constructor(){
        super();
        this.clsName = this.constructor.name;
    }
    
    /**
     * 设置全局DI容器
     * @param container DI容器实例
     */
    static setContainer(container: DIContainer): void {
        component.container = container;
    }
    
    /**
     * 获取服务实例
     * @param token 服务标识
     * @returns 服务实例
     * @throws Error 如果服务不存在或DI容器未设置
     */
    protected getService<T>(token: string): T {
        if (!component.container) {
            throw new Error(`[${this.clsName}] DI Container not set. Cannot get service: ${token}`);
        }
        
        return component.container.get<T>(token);
    }
    
    /**
     * 检查服务是否可用
     * @param token 服务标识
     * @returns 是否可用
     */
    protected hasService(token: string): boolean {
        if (!component.container) return false;
        return component.container.has(token);
    }
    
    init(){

    }
    
    setBody(body:BaseBody,master:BaseBody | null = null){
        this._body = body;
        this.id = body.id;
        this._master = master;
    }
    
    getComponent(name:ComType):any{
        return this.getBody().getComponent(name);
    }
    
    client_text(text: string) {
        let clientCom = (this._master || this.getBody()).getComponent(ComType.client) as client_component;
        clientCom.client_text(text);
    }
    
    client_img(name:string,data:any){
        let clientCom = (this._master || this.getBody()).getComponent(ComType.client) as client_component;
        clientCom.client_img(name,data);
    }
    
    registerIntent(type:mathType,intent:string,callback:Function,isHideMath:boolean = false){
        let intent_com = this.getComponent(ComType.意图) as intent_component;
        intent_com.registerIntent(type,intent,callback,this,isHideMath);
    }
    
    Pop_close(){
        let pop = this.getComponent(ComType.对话框) as any;
        pop.close()
    }
    
    dev(){
        this.client_text('开发中功能');
    }
    
    fighting_end(){
        let rpg_com = this.getComponent(ComType.rpg);
        rpg_com._clientHandler.showPowerChange();
    }
    
    public getBody(): BaseBody {
        return this._body;
    }
    
    // 通用按钮意图注册方法 - 消除重复代码
    protected registerButtonIntents(card: Icard, buttonConfigs: ButtonConfig[]): void {
        IntentHelper.registerButtonIntents(buttonConfigs, this.registerIntent.bind(this), this, card);
    }
    
    // 通用场景意图注册方法
    protected registerSceneIntents(intentConfigs: Array<{
        command: string;
        action: string;
        mathType: mathType;
        isHideMath: boolean;
    }>): void {
        intentConfigs.forEach(config => {
            this.registerIntent(config.mathType, config.command, (this as any)[config.action], config.isHideMath);
        });
    }
    
    // 通用条件意图注册方法 - 处理复杂的条件逻辑
    protected registerConditionalIntents(configs: Array<{
        command: string;
        action: string | Function;
        mathType: mathType;
        isHideMath?: boolean;
    }>): void {
        configs.forEach(config => {
            const action = typeof config.action === 'string' ? (this as any)[config.action] : config.action;
            this.registerIntent(config.mathType, config.command, action, config.isHideMath || false);
        });
    }
}

